package com.jack.charging.order.mqtt;

import com.jack.charging.common.pojo.utils.JsonUtils;
import com.jack.charging.order.common.ChargingConstants;
import com.jack.charging.order.common.MqttConstants;
import com.jack.charging.order.common.OrderStatusContants;
import com.jack.charging.order.pojo.dto.ChargingResultDto;
import com.jack.charging.order.pojo.po.ChargingBillFail;
import com.jack.charging.order.pojo.po.ChargingBillSuccess;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;


/**
 * @author Jack
 * @date 2025/2/12 12:39
 * @see
 * @since
 */
@Slf4j
public class MqttConsumer implements MqttCallbackExtended {

    private MqttClient client;
    private MqttContext mqttContext;

    public MqttConsumer(MqttClient mqttClient, MqttContext mqttContext) {
        this.client = mqttClient;
        this.mqttContext = mqttContext;
    }

    /**
     * 连接成功
     *
     * @param reconnect 是否是重连
     * @param serverURI 服务器地址
     */
    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        log.debug("connectComplete:reconnect:{},serverURI:{}", reconnect, serverURI);

        try {
            log.debug("subscribe:{}", MqttConstants.TOPIC_CHARGING_RESULT);
            client.subscribe(MqttConstants.TOPIC_CHARGING_RESULT);
            log.debug("subscribe success");
        } catch (Exception e) {
            log.error("subscribe error", e);
        }
    }

    /**
     * 连接断开
     *
     * @param throwable 异常
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.debug("连接断开，throwable:{}", throwable);
    }
    /**
     * 消息到达
     *
     * @param s         主题
     * @param mqttMessage 消息
     * @throws Exception 异常
     */

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        log.debug("messageArrived:s:{},mqttMessage:{}", s, mqttMessage);
        ChargingResultDto chargingResultDto = null;
        try {
            chargingResultDto = JsonUtils.fromJson(new String(mqttMessage.getPayload()), ChargingResultDto.class);
            log.debug("chargingResultDto:{}", chargingResultDto);
        }catch (Exception e){
            log.error("messageArrived error", e);
        }
        if (chargingResultDto != null){
            log.debug("chargingResultDto:{}", chargingResultDto);
            String result= chargingResultDto.getResult();

            if(ChargingConstants.CHARGING_RESULT_SUCCESS.equals(result)){
                ChargingBillSuccess chargingBillSuccess = new ChargingBillSuccess();
                chargingBillSuccess.setBillId(chargingResultDto.getOrderNo());
                chargingBillSuccess.setBillStatus(OrderStatusContants.ORDER_STATUS_CHARGING);
                chargingBillSuccess.setGunId(chargingResultDto.getPileId());
                log.debug("chargingBillSuccess:{}", chargingBillSuccess);
                int row = mqttContext.getOrderSuccessMapper().insert(chargingBillSuccess);
                log.debug("chargingBillSuccess:{},row:{}", chargingBillSuccess, row);
            }else {
                log.debug("开始充电失败",result);
                ChargingBillFail chargingBillFail = new ChargingBillFail();
                chargingBillFail.setBillId(chargingResultDto.getOrderNo());
                chargingBillFail.setFailDesc("设备没有响应");
                chargingBillFail.setGunId(chargingResultDto.getGunId());
                log.debug("chargingBillFail:{}", chargingBillFail);
                int row = mqttContext.getOrderFailMapper().insert(chargingBillFail);
                log.debug("chargingBillFail:{},row:{}", chargingBillFail, row);
            }
        }
    }
    /**
     * 消息发送完成
     *
     * @param iMqttDeliveryToken 消息
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.debug("deliveryComplete:iMqttDeliveryToken:{}", iMqttDeliveryToken);
    }
}
